package com.hsj.busmanager.service;

import com.hsj.busmanager.api.News;
import com.hsj.busmanager.api.UserBean;
import com.hsj.busmanager.mapper.NewsMapper;
import com.hsj.busmanager.util.CommonUtil;
import com.hsj.busmanager.util.Page;
import com.hsj.busmanager.util.TabelData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class NewsService {

    @Autowired
    private NewsMapper newsMapper;

    @Autowired
    private UserService userService;

    public List<News> getNewsByUserId(Integer userId) {

        return newsMapper.getNewsByUserId(userId);

    }

    public TabelData<News> getNewsPage(Page page,Integer userId) {

        TabelData tabelData = new TabelData();

        Integer start = (page.getPage()-1)*page.getLimit();

        List<News> newList = newsMapper.getNewsPage(start,page.getLimit(),userId);

        for(News news:newList){
            if(news.getCreateTime()!=null){
                news.setShowCreateTime(CommonUtil.getDateByFormat(news.getCreateTime(),"yyyy-MM-dd"));
            }
        }

        int count = newsMapper.getNewsCount(userId);

        tabelData.setCount(count);
        tabelData.setData(newList);

        return tabelData;


    }

    public void update(News news) {
        newsMapper.updateByPrimaryKeySelective(news);
    }

    public void send(String title, String content, int sendUserId, Integer rechiveUserId) {

        News news = new News();
        news.setIsRead("0");
        news.setCreateTime(new Date());
        news.setContent(content);
        news.setTitle(title);
        news.setSendUser(sendUserId+"");
        news.setUser(rechiveUserId+"");
        news.setType("1");
        newsMapper.insert(news);
    }

    public int getUnReadCount(Integer userId) {

        return newsMapper.getUnReadCount(userId);
    }

    public TabelData<News> getNoticePage(Page page, News news) {

        TabelData tabelData = new TabelData();

        Integer start = (page.getPage()-1)*page.getLimit();

        List<News> userList = newsMapper.getAdminNewsPage(start,page.getLimit(),news);

        if(userList!=null & userList.size()>0){
            for(News n:userList){
                n.setShowCreateTime(CommonUtil.getDateByFormat(n.getCreateTime(),"yyyy-MM-dd HH:mm:ss"));
            }
        }

        int count = newsMapper.getAdminNewsCount(news);

        if (userList == null && userList.size() == 0) {
            return null;
        }

        tabelData.setCount(count);
        tabelData.setData(userList);

        return tabelData;

    }

    public void insert(News news,String sendUserId) {

        if(news.getUser().equals("0") ){
//            全部发送
            List<UserBean> users = userService.getDriverList();
            News n = null;
            if(users!=null && users.size()>0){
                for(UserBean u:users){
                    n = new News();
                    n.setUser(u.getId()+"");
                    n.setSendUser(sendUserId);
                    n.setIsRead("0");
                    n.setCreateTime(new Date());
                    n.setType("2");
                    n.setTitle(news.getTitle());
                    n.setContent(news.getContent());
                    newsMapper.insert(n);
                }
            }
        }else{
            news.setSendUser(sendUserId);
            news.setType("2");
            news.setCreateTime(new Date());
            news.setIsRead("0");
            newsMapper.insert(news);
        }


    }

    public void deleteById(Integer id) {
        newsMapper.deleteByPrimaryKey(id);
    }

    public Object getById(Integer id) {
        return  newsMapper.selectByPrimaryKey(id);
    }
}
